const app = getApp();

var type = 'add';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'add',
    info: null,
    pics: [],
    card_id: '',
    goods_id:'',
    meal_type:1,
    status:'',
    title:''
  },
  backto: function (e) {
    wx.navigateBack({})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    if (options.type) {
      type = options.type;
    if(options.status == 1){
      that.data.title = '编辑充次套餐';
    }else{
      that.data.title = '编辑充值套餐';
    }

      

      
      var url = 'https://www.58zltc.com/api.php/Card/getMealAddForm/openid/' + app.globalData.openid + '/card_id/' + options.card_id + '/id/' + options.id + '/status/' + options.status;
    } else {

      if (options.status == 1) {
        that.data.title = '添加充次套餐';
      } else {
        that.data.title = '添加充值套餐';
      }
     
      var url = 'https://www.58zltc.com/api.php/Card/getMealAddForm/openid/' + app.globalData.openid + '/card_id/' + options.card_id +'/status/'+options.status;
    }
    app.ihttpGet(url, function (res) {
      that.setData({
        input_list: res.data.form,
        add_url: res.data.add_url,
        token: token,
        card_id: options.card_id,
        goods:res.data.goods,
        title: that.data.title
      })
    })
  },
  bindChange: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].value = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
  checkboxChange: function (e) {
    var good_id = '';
    
    for (var i = 0; i < e.detail.value.length;i++) {
      good_id = good_id + e.detail.value[i];
      good_id = good_id +',';
     }
    
   this.data.goods_id=good_id;
    
  },
 
  // 发货地址选择,获取用户选择的单选框的值
  radioChange: function (e) {
    this.data.meal_type = e.detail.value;
  },


  formSubmit: function (e) {
    app.addFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    let that = this;

    post_data.openid = app.globalData.openid;
    post_data.card_id = data.card_id;
    post_data.goods = data.goods_id;
    post_data.type = data.meal_type;
    if (data.type == 'edit') {
      post_data.id = data.info.id;
    }
    app.ihttpPost(data.add_url, post_data, function (res) {
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack({})
        }, 500)
      }
    })
  },
})